package com.example.xViewChat.UI.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.xViewChat.Bean.IntentBean;
import com.example.xViewChat.NettyClient.PushManager;
import com.example.xViewChat.R;
import com.example.xViewChat.UI.Adapter.FriendListAdapter;
import com.example.xViewChat.UI.ChatActivity;
import com.example.xViewChat.UI.SearchFriendActivity;
import com.example.xViewChat.UI.View.TitleBarView;
import io.ganguo.chat.core.protocol.Commands;
import io.ganguo.chat.core.protocol.Handlers;
import io.ganguo.chat.core.transport.Header;
import io.ganguo.chat.core.util.JacksonMapper;
import io.ganguo.chat.route.biz.entity.User;

import java.util.List;

/**
 * Created by hasee on 2016-04-14.
 * 目前的策略是这样，所有的好友信息从本地数据库获取，只有添加好友、手动刷新或者第一次家在才从服务器获取。
 */
public class FriendListFragment extends EventListenerFragment  {
    private Context mContext;
    private View mBaseView;
    private TitleBarView mTitleBarView;
    private ListView mFriendListView;

    private List<User> mFriendList;
    private Handler handler;
    private FriendListAdapter adapter;

    static  LocalBroadcastManager broadcastManager=null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        super.onCreateView(inflater,container,savedInstanceState);
        /*if (broadcastManager == null){
            broadcastManager = LocalBroadcastManager.getInstance(getActivity());
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.intent.action.CART_BROADCAST");//建议把它写一个公共的变量，这里方便阅读就不写了。
            BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    System.out.println("OK");
                }
            };
            broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
        }
        Intent intent = new Intent("android.intent.action.CART_BROADCAST");
        LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);*/
        mContext = getActivity();
        mBaseView = inflater.inflate(R.layout.fragment_friendlist, null);
        System.out.println("初始化friendListFragment");
        findView();
        getList();
        return mBaseView;
    }

    @Override
    public void onStart(){
        System.out.println("```````````````friendListFragment in onstart()");
        super.onStart();
    }

    private void init(IntentBean intentBean) {
       mFriendList = JacksonMapper.readListValue((String)intentBean.getEntity(),User.class);


        adapter = new FriendListAdapter(mContext, mFriendList);
        mFriendListView.setAdapter(adapter);

        mTitleBarView.setCommonTitle(View.VISIBLE, View.VISIBLE, View.VISIBLE);
        mTitleBarView.setTitleText("好友");
        mTitleBarView.setBtnLeft(R.string.control);
        mTitleBarView.setBtnRight(R.drawable.qq_constact);

        mFriendListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                User friend = mFriendList.get(position);
                Intent intent = new Intent(mContext, ChatActivity.class);
                intent.putExtra("friendName", friend.getNickName());
                intent.putExtra("friendId", friend.getId());
                startActivity(intent);
            }
        });
        mTitleBarView.setBtnRightOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SearchFriendActivity.class);
                startActivity(intent);
            }
        });
    }

    private void getList() {
        IntentBean intentBean=new IntentBean();
        Header header=new Header();
        header.setHandlerId(Handlers.OPERATION);
        header.setCommandId(Commands.GET_PRESENCE);
        intentBean.setHeader(header);
        PushManager.send(mContext, intentBean);
    }

    private void findView() {
        mTitleBarView = (TitleBarView) mBaseView.findViewById(R.id.friend_title_bar);
        mFriendListView = (ListView)mBaseView.findViewById(R.id.friend_list_listview);
    }

    @Override
    public void onReplyReceived(IntentBean intentBean) {
        switch (intentBean.getHeader().getCommandId()) {
            case Commands.GET_PRESENCE_SUCCESS://获取在线成功。刷新List
                System.out.println("来到了 FriendListFragment，将要刷新当前所有的User表格");
                init(intentBean);
                break;

        }
    }

    @Override
    public void onStop(){
        super.onStop();
    }


}
